package com.lineage.server.command.executor;

import com.lineage.data.event.gambling.Gambling;
import com.lineage.data.event.gambling.GamblingNpc;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.timecontroller.event.GamblingTime;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1Gam implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1Gam.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1Gam();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      Gambling gambling = GamblingTime.get_gambling();
      if (pc == null) {
        if (arg.equalsIgnoreCase("show")) {
          _log.info("系统预设:" + gambling.WIN + ":");
          for (Integer key : gambling.get_allNpc().keySet()) {
            GamblingNpc gam = (GamblingNpc)gambling.get_allNpc().get(key);
            _log.info("跑道:" + gam.get_xId() + "/" + gam.get_npc().getName());
          } 
        } else if (arg.equalsIgnoreCase("start")) {
          GamblingTime.set_status(true);
          _log.info("系统:允许比赛接续执行");
        } else if (arg.equalsIgnoreCase("stop")) {
          GamblingTime.set_status(false);
          _log.info("系统:中止比赛接续执行");
        } else if (arg.startsWith("change")) {
          int xid = Integer.parseInt(arg.substring(4));
          if (xid > 4) {
            _log.info("错误!跑道编号为0~4");
            return;
          } 
          gambling.WIN = xid;
          _log.info("变更设置:" + gambling.WIN);
          for (Integer key : gambling.get_allNpc().keySet()) {
            GamblingNpc gam = (GamblingNpc)gambling.get_allNpc().get(key);
            _log.info("跑道:" + gam.get_xId() + "/" + gam.get_npc().getName());
          } 
        } 
      } else if (arg.equalsIgnoreCase("show")) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "系统预设:" + gambling.WIN));
        for (Integer key : gambling.get_allNpc().keySet()) {
          GamblingNpc gam = (GamblingNpc)gambling.get_allNpc().get(key);
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "跑道:" + gam.get_xId() + "/" + gam.get_npc().getName()));
        } 
      } else if (arg.equalsIgnoreCase("start")) {
        GamblingTime.set_status(true);
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "系统:允许比赛接续执行"));
      } else if (arg.equalsIgnoreCase("stop")) {
        GamblingTime.set_status(false);
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "系统:中止比赛接续执行"));
      } else if (arg.startsWith("change")) {
        int xid = Integer.parseInt(arg.substring(4));
        if (xid > 4) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "错误!跑道编号为0~4"));
          return;
        } 
        gambling.WIN = xid;
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "变更设置:" + gambling.WIN));
        for (Integer key : gambling.get_allNpc().keySet()) {
          GamblingNpc gam = (GamblingNpc)gambling.get_allNpc().get(key);
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "跑道:" + gam.get_xId() + "/" + gam.get_npc().getName()));
        } 
      } 
    } catch (Exception e) {
      if (pc == null) {
        _log.error("错误的命令格式: " + getClass().getSimpleName());
      } else {
        _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
      } 
    } 
  }
}
